package com.fragment;

import java.util.ArrayList;

import org.apache.http.Header;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.act.Web_CommonAct;
import com.bean.ServiceItem;
import com.bean.ServiceList;
import com.common.Constant;
import com.loopj.android.http.TextHttpResponseHandler;
import com.mUI.XListView;
import com.mUI.XListView.IXListViewListener;
import com.umeng.analytics.MobclickAgent;
import com.xike.kyt.R;
import com.xike.util.MLog;
import com.xike.util.MyUtil;

public class ServiceFragment extends BaseFragment {

	XListView listView;
	ServiceList serviceList;
	ArrayList<ServiceItem> services = new ArrayList<ServiceItem>();
	boolean isRefresh = false;
	MyAdapter adapter;

	Bitmap defaultBitmap;
	int pageCursor = 1;
	boolean isLoadingMore = false;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.frag_service, null);
		initViews(view);
		return view;
	}

	private void initViews(View view) {
		listView = (XListView) view.findViewById(R.id.service_listView);
		listView.setPullRefreshEnable(true);
//		listView.setPullLoadEnable(true);
//		listView.setAutoLoadEnable(true);

		adapter = new MyAdapter();
		listView.setAdapter(adapter);

		listView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				pageCursor = 1;
				isRefresh = true;
				serviceGetServiceList();
			}

			@Override
			public void onLoadMore() {
//				pageCursor++;
//				isLoadingMore = true;
//				serviceGetServiceList();
			}
		});

		BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.default_user_avator);
		defaultBitmap = MyUtil.getRoundedCornerBitmap(bd.getBitmap());

		serviceGetServiceList();
	}

	private void updateView() {
		ArrayList<ServiceItem> loadService = serviceList.getServices();
		if (loadService == null || loadService.size() <= 0) {
			if (pageCursor == 1) {
				mToast.showToast(getActivity(), "暂无数据");
			}
		} else {

			if (isRefresh) {
				services.clear(); // 刷新 清空
				services.addAll(loadService);
				adapter.notifyDataSetChanged();
				listView.stopRefresh();
				listView.updateRefreshTime();
				isRefresh = false;
				listView.setPullLoadEnable(true);
			} else {
				services.addAll(loadService);
				adapter.notifyDataSetChanged();
			}
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					ServiceItem item = services.get(position - listView.getHeaderViewsCount());
					Intent it = new Intent(getActivity(), Web_CommonAct.class);
					it.putExtra("url", item.getUrl());
					it.putExtra("title", item.getTitle());
					startActivity(it);

				}

			});
		}

	}

	private void serviceGetServiceList() {

		String strUrl = MyUtil.signRequestUrl(Constant.SERVER_URL_SERVICE_LIST, null);
		client.get(getActivity(), strUrl, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				ServiceList result = gson.fromJson(responseString, ServiceList.class);
				MLog.e("serviceGetIndex", "result:" + responseString);
				dealRequestResult(result);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(getActivity(), getResources().getString(R.string.net_error));
			}
		});
	}

	private void dealRequestResult(ServiceList result) {
		if (result != null) {
			if (result.isReturnTrue()) {
				serviceList = result;
				updateView();
			} else {
				mToast.showToast(getActivity(), result.getError_description());
			}
		} else {
			mToast.showToast(getActivity(), getString(R.string.net_error));
		}
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return services.size();
		}

		@Override
		public ServiceItem getItem(int position) {
			return services.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(getActivity(), R.layout.listitem_service, null);
				holder = new ViewHolder();
				holder.tv_title = (TextView) convertView.findViewById(R.id.lvitem_service_tv_title);
				holder.tv_desc = (TextView) convertView.findViewById(R.id.lvitem_service_tv_desc);
				holder.img = (ImageView) convertView.findViewById(R.id.lvitem_service_img);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			ServiceItem item = getItem(position);
			MyUtil.setTextViewText(holder.tv_title, item.getTitle(), 0, "");
			MyUtil.setTextViewText(holder.tv_desc, item.getDescription(), View.GONE, "");

			MyUtil.displayImageView(getActivity(), item.getImage(), holder.img, defaultBitmap, true);
			return convertView;
		}
	}

	private static class ViewHolder {
		TextView tv_title;
		TextView tv_desc;
		ImageView img;
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("ServiceFragment"); // 统计页面
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("ServiceFragment");
	}
}
